package com.caiyouxi.finance.controller;

import com.alibaba.fastjson.JSONObject;
import com.caiyouxi.common.bean.ResData;
import com.caiyouxi.common.control.BaseController;
import com.caiyouxi.finance.model.Coupons;
import com.caiyouxi.finance.model.CouponsPayBean;
import com.caiyouxi.finance.service.CouponsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by Administrator on 2017/10/19.
 */
@RestController
@RequestMapping("/coupons")
public class CouponsController extends BaseController {

    @Autowired
    private CouponsService couponsService;

    /**
     * 创建
     * @param request
     * @param response
     * @param bean
     * @return
     */
    @RequestMapping("add")
    public ResData add_order(HttpServletRequest request, HttpServletResponse response, @RequestBody Coupons bean) {
        return  success(bean);
    }

    /**
     * 查询券信息
     */
    @RequestMapping("/get_coupons")
    public ResData get_coupons(HttpServletRequest request, HttpServletResponse response, @RequestBody Coupons bean){
        Coupons coupons = couponsService.select_coupons(bean.getId());
        JSONObject data = (JSONObject) JSONObject.toJSON(coupons);
        return success(data);
    }

    /**
     * 获取所有券信息
     */
    @RequestMapping("/get_coupons_list")
    public ResData get_coupons_list(HttpServletRequest request, HttpServletResponse response){
        List<Coupons> couponsList = couponsService.get_coupons_list();
//        JSONObject data = (JSONObject) JSONObject.toJSON(couponsList);
        return success(couponsList);
    }

    /**
     * 获取所有有效的券信息
     * fund_type该券可以用在什么样的资金用途上
     */
    @RequestMapping("/get_coupons_usefull_list")
    public ResData get_coupons_usefull_list(HttpServletRequest request, HttpServletResponse response, @RequestBody CouponsPayBean bean){
        List<Coupons> couponsList = couponsService.get_coupons_usefull_list(bean);
//        JSONObject data = (JSONObject) JSONObject.toJSON(couponsList);
        return success(couponsList);
    }

    /**
     * 分页查询所有信息(wushihao 2017-11-28)
     * */
    @RequestMapping("select_coupons_list")
    public ResData select_coupons_list(@RequestBody Coupons bean){
        return couponsService.select_coupons_list(bean);
    }

    /**
     *  更新券信息(wushihao 2017-11-28)
     *  查看返回值以确定是否更新完毕
     * */
    @RequestMapping("update_coupons_override")
    public ResData update_coupons_override(@RequestBody Coupons bean){
        return couponsService.update_coupons_override(bean);
    }
}
